package {
   import com.ease.core.managers.PerspectiveManager;
   import com.ease.core.managers.ViewManager;
   import com.ease.core.model.ViewDefine;
   import com.ease.util.ObjectEvent;
   import com.ease.framework.CommandCompletionEvent;
   import com.obas.commands.DefaultErrorHandler;
   import com.obas.events.ReLoginEvent;
   import com.obas.events.RequestPluginsEvent;
   import com.obas.utils.FlashVariableLoader;
   import com.obas.views.MainWindow;
   import com.ease.model.term.CoolPerspective;
   import com.ease.model.term.CoolView;
   import com.ease.plugin.PluginFactory;
   import com.ease.plugin.URLResource;
   import com.ease.plugin.events.FactoryEvent;

   import flash.events.EventDispatcher;

   import mx.collections.ArrayCollection;
   import mx.core.FlexGlobals;

   [Event(name="{com.obas.events.RequestPluginsEvent.REQUEST_ID}",
      type="com.obas.events.RequestPluginsEvent")]

   [Event(name="{com.obas.events.ReLoginEvent.RELOGIN_SUCCESS}",
      type="com.obas.events.ReLoginEvent")]

   public class MainApplicationMediator extends EventDispatcher {
      private var _ready:Boolean = false;

      private var _view:MainApplication;

      [InjectableView]
      /**
       * The mediator's view.
       */
      public function get view():MainApplication {
         return _view;
      }

      public function set view(value:MainApplication):void {
         _view = value;
         FlexGlobals.topLevelApplication.dispatchEvent(ObjectEvent.newAddition(DefaultErrorHandler.getInstance()));
         FlashVariableLoader.loaderParameters(view.loaderInfo);
//         var factory:PluginFactory = new PluginFactory();
//         factory.addEventListener(FactoryEvent.PLUGIN_LOAD,onPluginLoad);
//
//         var myPluginURL:URLResource = new URLResource("TestLight.swf");
//         myPluginURL.encrypted = true;
//         myPluginURL.key = "aaa";
//         factory.loadPlugin(myPluginURL);
//
//         function onPluginLoad(event:FactoryEvent):void{
//            event.resource;
//         }

         loadConfig();
      }

      private function loadConfig():void {
         PerspectiveManager.getInstance().clear();
         ViewManager.getInstance().clear();

         dispatchEvent(new RequestPluginsEvent());
         dispatchEvent(new RequestPluginsEvent(RequestPluginsEvent.TYPE_GET_VIEW));
      }

      [ResponseHandler("{com.obas.events.RequestPluginsEvent.RETURN_ID}")]
      public function onRequestPluginsEvent(event:CommandCompletionEvent):void {
         if (event.error) {
            return;
         }
         var request:RequestPluginsEvent = event.request as RequestPluginsEvent;

         if (request.actionType == RequestPluginsEvent.TYPE_GET_PERSPECTIVE) {
            var perspectives:ArrayCollection = event.result as ArrayCollection;
            for each (var perspective:CoolPerspective in perspectives) {
               PerspectiveManager.getInstance().addPerspective(perspective.code, perspective);
               PerspectiveManager.getInstance().addPerspectiveDefine(perspective.code, "com.ease.plugin.irm.factories.PerspectiveFactory");
            }
            if (!_ready) {
               _ready = true;
            }
            else {
               loadComplete();
            }
         }
         else if (request.actionType == RequestPluginsEvent.TYPE_GET_VIEW) {
            var views:ArrayCollection = event.result as ArrayCollection;
            for each (var view:CoolView in views) {
               var viewDef:ViewDefine = new ViewDefine();
               viewDef.id = view.code;
               viewDef.label = view.name;
               viewDef.type = view.type;
               viewDef.icon = view.icon;
               viewDef.location = view.location;
               viewDef.index = view.ordinal;
               ViewManager.getInstance().addViewDefine(view.code, viewDef);
            }
            if (!_ready) {
               _ready = true;
            }
            else {
               loadComplete();
            }
         }
      }

      [EventHandler("{com.obas.events.ReLoginEvent.RELOGIN_SUCCESS}")]
      public function onReLoginEvent(event:ReLoginEvent):void {
         loadConfig();
      }

      private function loadComplete():void {
         view.appContainer.removeAllChildren();
         ViewManager.getInstance().removeAllViewInstance();
         _view.appContainer.addChild(new MainWindow());
      }
   }
}